{template common/contentheader}
<div class="setcontent">
	<div class="settitle">
		<h3>{lang taobao_gather:admin}</h3>{template operation/taobao-submenu}
	</div>
	<div class="alert alert-info mt10">
		<span class="label label-info">{lang info:admin}</span>
		{lang taobao_gather_tip:admin}
	</div>
	<div class="setform">
		<div class="alert">
			<form action="{url admin/taobaogather?act=search}" method="post">
			<div class="input-append">
				关键字:<input class="span3" type="text" name="keyword" id="keyword" value="$keyword" placeholder="关键字">
				<select id="channel_category_id" name="channel_category_id">
					  <option value="0" {echo ($channel_category_id==0)?'selected':''}>所有分类</option>
					  {if $channel_categories}
					  {loop $channel_categories $category}
					  <option value="{echo $category->cid}" {echo ($channel_category_id==$category->cid)?'selected':''}>{echo $category->name}</option>
					  {/loop}
					  {/if}
				</select>
				<button type="submit" class="btn btn-primary submit-btn">查询</button>
			</div>
			</form>
		</div>
		
		<div class="alert alert-info">
			<form id="publish_form" action="{url admin/taobaogather?act=publish}" method="post">
			<div class="input-append">
				<span class="label label-info">发布至分类</span>
			 		<select id="category_id" name="category_id">
						  {loop $rootCategories $category}
						  <option value="$category['category_id']">$category['category_name_cn']</option>
						  {/loop}
					</select>
					<span class="label label-info">专辑</span>
					<select id="album_id" name="album_id">
					</select>
					是否多图:
					<input type="radio" name="morepic" value="1">{lang yes:admin}</input>
              		<input type="radio" name="morepic" value="0">{lang no:admin}</input>
					<input id="num_iid_str" name="num_iid_str" type="hidden"/>
			        <button type="submit" class="btn btn-primary">发布</button> 
			</div>
			</form>
		</div>
		<div class="alert alert-danger" id="ajax_message"></div>
	</div>
	<div class="setform">
		<table class="table table-striped">
        <thead>
          <tr>
            <th width="40"><input type="checkbox" id="select_all" /></th>
            <th width="50">商品名称</th>
            <th width="400">商品名称</th>
            <th width="100">价格</th>
            <th width="100">比率</th>
            <th width="110">佣金</th>
            <th width="200">推广信息</th>
          </tr>  
        </thead>
        <tbody>
        {loop $items['items'] $item}
			<tr>
			<td><input type="checkbox" value="{echo  $item->num_iid}"></td>
			<td><div class="img_border"><img width="32" data-bimg="{$item->pic_url}_200x200.jpg" class="J_preview" src="{echo  $item->pic_url.'_100x100.jpg'}"></div></td>
			<td><a href="{echo  $item->click_url}" target="_blank">{echo  $item->title}</a></td>
			<td align="center">{echo  $item->price}</td>
            <td align="center">{echo  round(($item->commission_rate/100),2)}%</td>
              <td align="center">{echo  $item->commission}元</td>
              <td align="center">30天推广量{echo  $item->commission_num}件<br />
            30天推广佣金{echo  $item->commission_volume}元</td>
            </tr>
		{/loop}
        </tbody>
		</table>
		<div class="text_c">$pages</div>
	</div>
</div>
<script type="text/javascript">
$(document).ready(function($) {
	var interval;
	function get_channel_categories() {
		var purl = '{url admin/taobaogather?act=fetch_category}';
		$("#ajax_message").html("正在获取淘宝分类，请稍后....");
	    interval = window.setInterval(function(){
			var text = $("#ajax_message").html();
			if (text.length < 32){
				$("#ajax_message").html(text + '.');					
			} else {
				 $("#ajax_message").html("正在获取淘宝分类，请稍后....");		
			}
		}, 200);
		$.ajax({
			type: "get",
			dataType: 'json',
			url: purl
		}).error(function() {
			window.clearInterval(interval);
			alert('获取淘宝分类失败');
		}).success(function(result) {
			window.clearInterval(interval);
			if (result.success === !0) {
				var lis = $(Mustache.render('<option value="0">所有分类</option>{{#data}}<option value="{{cid}}">{{name}}</option>{{/data}}', result));
				$('#channel_category_id').html(lis);
				 $("#ajax_message").html("分类获取成功，赶快发布吧。");		
			} else {
				$('#channel_category_id').html('');
				 $("#ajax_message").html("分类获取失败，请检查您的淘宝KEY以及服务器网络状态。");		
			}
		});
	}
	
	function getAlbumList(cat_id) {
		var purl = '{url album/listuseralbums}';
		$.ajax({
			type: "get",
			dataType: 'json',
			data: {'cid':cat_id},
			url: purl
		}).error(function() {
			alert('获取专辑失败');
		}).success(function(result) {
			if (result.success === !0) {
				var lis = $(Mustache.render('{{#data}}<option value="{{album_id}}">{{album_title}}</option>{{/data}}', result));
				$('#album_id').html(lis);
			} else {
				$('#album_id').html('');
			}
		});
	}
	$('#publish_form').validate({
		submitHandler: function(form) {
			var num_iid_str = '';
			$(':checkbox').each(function() {
				var checked = $(this).attr("checked");
				var num_iid = $(this).val();
				if(checked=='checked'){
					num_iid_str += num_iid+',';
				}
	        });
			if(num_iid_str==null||num_iid_str==""){
				alert("您没有内容需要发布，请选择。");
				return false;
			}
	        $("#num_iid_str").val(num_iid_str);
			$('#publish_form').ajaxSubmit({
					url:  $('#publish_form').attr('action'),
					type: 'POST',
					dataType: 'json',
					beforeSubmit: function(){
					    $("#ajax_message").html("发布中，多图采集过程可能稍慢，请耐心等候....");
					    interval = window.setInterval(function(){
							var text = $("#ajax_message").html();
							if (text.length < 55){
								$("#ajax_message").html(text + '.');					
							} else {
								 $("#ajax_message").html("发布中，多图采集过程可能稍慢，请耐心等候....");		
							}
						}, 200);
					},
					success: function(result) {
						 window.clearInterval(interval);
						 if (result.success === !0) {
							 $("#ajax_message").html("恭喜你，发布成功");
						 }else {
							 $("#ajax_message").html("发布失败....");
						 }
					}
			});
			return false;
		}
	});
	$('#select_all').live('click', function(){
		$(':checkbox').attr('checked', this.checked);
	});
	$('#category_id').change(function (){
		getAlbumList(this.value);
	});
	getAlbumList($('#category_id').val());
	get_channel_categories();
});
</script>
{template common/contentfooter}